package cn.edu.cug.cs.gtl.gsm.item;

import cn.edu.cug.cs.gtl.common.Identifier;
import cn.edu.cug.cs.gtl.io.Storable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;

public class DataItem implements Storable {
    Identifier pageIdentifier;
    byte [] pageContent;

    public Identifier getPageIdentifier() {
        return pageIdentifier;
    }

    public void setPageIdentifier(Identifier pageIdentifier) {
        this.pageIdentifier = pageIdentifier;
    }

    public byte[] getPageContent() {
        return pageContent;
    }

    public void setPageContent(byte[] pageContent) {
        this.pageContent = pageContent;
    }

    public DataItem(Identifier pageIdentifier, byte[] pageContent) {
        this.pageIdentifier = pageIdentifier;
        this.pageContent = pageContent;
    }

    @Override
    public Object clone() {

        DataItem dt = new DataItem((Identifier) pageIdentifier.clone(),
                Arrays.copyOf(pageContent,pageContent.length));
        return dt;
    }

    @Override
    public boolean load(DataInput dataInput) throws IOException {
        pageIdentifier.load(dataInput);
        dataInput.readFully(pageContent);
        return false;
    }

    @Override
    public boolean store(DataOutput dataOutput) throws IOException {
        pageIdentifier.store(dataOutput);
        dataOutput.write(pageContent);
        return true;
    }
}
